<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType="text/html; charset=big5" language="java" import="util.*, java.sql.*,beans.*, java.util.*, java.text.*" errorPage=""%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <SCRIPT LANGUAGE="JavaScript">            
            
            var rate, totalAmount, dollarSign, total;
            
            function calculate() {            
            document.getElementById("result").value = "Waiting...";
            document.getElementById("proceed").disabled = true;
            totalAmount = document.getElementById("HKDtotalAmount").value;
            dollarSign = document.getElementById("dollarSign").value;
            
            if(window.ActiveXObject) {
            rate = new ActiveXObject("Microsoft.XMLHTTP");
            } else if(window.XMLHttpRequest) {
            rate = new XMLHttpRequest();
            }
            rate.onreadystatechange = handleServerResponse; 
            rate.open('GET', 'CCServlet?sign='+dollarSign);
            rate.send(null);
            }
            
            
            function handleServerResponse() {
            if(rate.readyState == 4) { // the state when the request has completed
            if (rate.status == 200) { // the HTTP status code returned from the server
            var response = rate.responseXML;  
            var msg = response.getElementsByTagName("rate")[0].firstChild.nodeValue;             
            //alert(dollarSign+" "+msg*totalAmount);
            total = msg*totalAmount;
            document.getElementById("result").value = dollarSign+" "+total;
            document.getElementById("exchangeSign").value = dollarSign;
            document.getElementById("totalAmount").value = total;
            document.getElementById("proceed").disabled = false;
            }
            }
            }

            
    </SCRIPT>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Shopping Cart</title>
    <link href="astyles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="absolute_title">Captain Hook's Store</div>
<div class="absolute_title2">
    <h5>
        <%
        String username = (String)session.getAttribute("username");
        
        if(username==null) {
            response.sendRedirect("NotLoginPage.jsp");
            return;
        }else {
        %>
        <h5>Hi!<%= username%><br/>
        <a href="logout.jsp"> Click here to logout</a></h5>
        <% }
        %>
    </h5>
</div>
<div class="wrap">
<div class="canvas">
<div class="left_column">
    <div class="header_left">
        <div class="header_leftrowone">&nbsp;</div>
        <div class="header_leftrowtwo"><img src="../image/cpt_hook_banner1.jpg" width="123" height="87" /></div>
        <div class="header_leftrowthree"></div>
    </div>
    <div class="leftnav">
        <ul>
            <li></li>
        </ul>
    </div>
    <div>
        <SCRIPT src="displayMyFlash1.js"></SCRIPT>
    </div>
</div>
<div class="right_column">
<div class="header_right">
    <div class="header_rightrowone">&nbsp;</div>
    <div class="header_rightrowtwo">&nbsp;</div>
    <div class="header_rightrowthree">
        <ul class="topmenu">
            <li><a title="Home" 
                   href="index.jsp">Home</a> | </li>
            <li><a title="Information" 
                   href="companybackground.html">Information</a> | </li>
            <li><a title="Forum" 
                   href="http://www.voy.com/206926/">Discussion Forum</a></li>
        </ul>
    </div>
</div>
<div class="content_wrapper">
<div class="content_left">
<div>

<h2>Shopping Cart</h2>
<center>
    Your chosen items are as follow:
</center>
<table width="auto" height="197" border="1">
    <tr bgcolor="#99ffc">
        <th width="52">Item ID</th>
        <th width="116">Name</th>
        <th width="69">Company</th>
        <th width="185">Image</th>
        <th width="63">Quantity</th>
        <th width="78">Price($)</th>
        <th width="91">Total Price</th>
        <th width="60">Delete</th>
    </tr>
    <%
    boolean flag = false;
    String title = "";
    String type = null;
    String company = "";
    String color = "";
    String scale = "";
    String imageURL = "";
    double price = 0.0;
    int quantity = 1;
    float totalAmount = 0.0f;
    Cookie[] cookies = request.getCookies();
    
    for (int i=0; i<cookies.length; i++) {
        Cookie myCookie = cookies[i];
        String name = myCookie.getName();
        if ( name.indexOf(".ID") != -1) {
            String itemID = myCookie.getValue();
            for (int j=0; j<cookies.length; j++) {
                myCookie = cookies[j];
                String value = myCookie.getValue();
                name = myCookie.getName();
                if ( name.indexOf(itemID) != -1) {
                    if ( name.indexOf("Name") != -1)
                        title = value;
                    if ( name.indexOf("imageURL") != -1)
                        imageURL = value;
                    if ( name.indexOf("Price") != -1)
                        price = Double.parseDouble(value);
                    if ( name.indexOf("Quantity") != -1)
                        quantity = Integer.parseInt(value);
                    if ( name.indexOf("Type") != -1)
                        type = value;
                    if ( name.indexOf("Company") != -1)
                        company = value;
                    if ( name.indexOf("scale") != -1)
                        scale = value;
                }
            }
            
            if (flag) {
                flag = false;
                color = "#9DC4EA";
            } else {
                flag = true;
                color = "#B2ECB3";
            }
            totalAmount += price*quantity;
            
            DecimalFormat df = new DecimalFormat("#.00");
    %>
    <tr bgcolor='<%= color%>'>
        <td><%= itemID%></td>
        <td><%= title%></td>
        <td><%= company%></td>
        <td align="center" valign="middle" bgcolor="<%= color%>"><a href='ItemDetail.jsp?imageURL=<%=imageURL%>'><img src='../image/<%=imageURL%>' alt="item"width="162" height="200" border="0"/></td>
        <td align="right"><%= quantity%></td>
        <td><%= df.format(price)%></td>
        <td><%= df.format(price*quantity)%></td>
        <form  action='deleteItem.jsp' method='get' >
            <input type="hidden" name="itemID" value='<%= itemID%>' />
            <td><input name="Submit" type='submit' value='Delete' />
            </td>
        </form>
    </tr>
    <%
        }
    }
    
    if (totalAmount != 0.0f) {
    %>
    <tr bgcolor="##FFCCFF">
        <td height="124" colspan="8" align="right"><p><font color="green">Total Price=HKD$<%=totalAmount%></font>
                <input name="hidden" type='hidden' id='HKDtotalAmount' value='<%=totalAmount%>' />
            </p>
            <p> <font color="green">Convert to:</font>
                <select name="dollarSign" id="dollarSign" onclick="return calculate();">
                    <option value="HKD" >HK Dollars </option>
                    <option value="USD" >US Dollars </option>
                    <option value="EUR" >Euro </option>
                    <option value="JPY" >Japanese Yen </option>
                </select>
                <input name="text" type='text' id='result' value='' readonly="readonly" />
                <!-- <font color=green></font></p></td></tr> -->
                <%       }
                %>
        </p></td>
    </tr>
</table>
<p> </p>
<form name="proceed1" id="proceed" action='disclaimer.jsp' method='get' >
    <input type='hidden' name="exchangeSign" id='exchangeSign' value='HKD' />
    <input type='hidden' name="totalAmount" id='totalAmount' value='<%=totalAmount%>' />
    </p>
    <p> |
        <input name="submit" type='submit' id="proceed" value='Proceed to Payment' />
    | <a href="Catalogue.jsp">Back to Catalogue</a> | </p>
</form>
</div>
<div class="post_btm">
</div>
<%
if (type == null) {
%>
<BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/>
<%
}%>
<%
if (type != null) {
%>
<p><strong>Consider Similiar Items:</strong></p>
<%
//----------------banner------------------------
final int MAX_MARQUEE = 5;

int size = ModelBean.getTotalRecordCountImage();

Connection con2 = util.ConnectionManager.connect();
Statement stmt2 = con2.createStatement();
ResultSet rs2 = stmt2.executeQuery("select imageURL as ii, name as product_name, price as product_price from model where type ='"+type+"'");

int numOfImg = 0;
if(size < MAX_MARQUEE) {
    numOfImg = size;
}else {
    numOfImg = MAX_MARQUEE;
}

List<String> result = new ArrayList<String>();
List<String> name = new ArrayList<String>();
List<String> pricez = new ArrayList<String>();
while (rs2.next()) {
    result.add(rs2.getString("ii"));
    name.add(rs2.getString("product_name"));
    pricez.add(rs2.getString("product_price"));
}
int[] randoms = new int[numOfImg];
Random r = new Random();
for (int i=0; i<randoms.length; i++) {
    if (result.size() == 0) {
        randoms[i] = r.nextInt(1);
    }else {
        randoms[i] = r.nextInt(result.size()-1);
    }
}

String[] urls = new String[numOfImg];
String[] titles = new String[numOfImg];
String[] prices = new String[numOfImg];
String url = null;
String product_title = null;
String product_price = null;
int x=0,y=0;
for(int i=0; i<result.size(); ++i) {
    x++;
    url = result.get(i);
    product_title = name.get(i);
    product_price = pricez.get(i);
    for(int j=0; j<randoms.length; ++j) {
        if(randoms[j] == x) {
            urls[y] = url;
            titles[y] = product_title;
            prices[y] = product_price;
            y++;
        }
    }
}
for (int k=0; k<urls.length; k++) {
    for (int l=0; l<urls.length; l++) {
        if ( k!=l && urls[k] == urls[l]) {
            urls[k] = null;
        }
    }
}

%>
<table border="0" cellspacing="2" cellpadding="2">
    <tr>
        <%  
        for( int i=0; i<urls.length; ++i) {
    if(urls[i] !=null) {
        out.println("<td><a href='ItemDetail.jsp?imageURL="+ urls[i] +"'><img height='180' width='150' src='../image/" + urls[i] + "'/></a></td>");
    }
        }
        %>
    </tr>
    <tr>
        <%      
        for( int i=0; i<urls.length; ++i) {
    if(urls[i] !=null) {
        out.println("<td>"+titles[i]+"</td>");
    }
        }
//--------------------------endof banner
        %>
    </tr>
    <tr>
        <% 
        
            DecimalFormat df = new DecimalFormat("#.00");
        for( int i=0; i<urls.length; ++i) {
    if(urls[i] !=null) {
        out.println("<td><font color='blue'>$"+df.format(Double.parseDouble(prices[i]))+"</font></td>");
    }
        }
//--------------------------endof banner
        %>
    </tr>
</table>
<% 
}%>  

</div>

<br>
<div class="rightbox_a"></div>

</div>
<div class="footer">
    <ul class="basemenu">
        <li></li>
    </ul>
    Designed by Captain Hook inc, all rights reserved. <br />
</div>
</div>
</div>
</div>
</body>
</html>
